﻿

#region Using Statements

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

#endregion


namespace Freeform.Framework.Graphics
{
    /// <summary>
    /// Actors are used to graphically represent an entity on a viewport.
    /// </summary>
    public class Actor_Image2D : Actor
    {

        #region Members

        /// <summary>
        /// Gets a reference to the animation set for this actor.
        /// </summary>
        public virtual AnimationSet Animations { get; private set; }

        #endregion


        #region Init

        /// <summary>
        /// Creates a new 2d image actor.
        /// </summary>
        /// <param name="Entity">The entity that this actor represents.</param>
        public Actor_Image2D(Entity Entity)
            : base(Entity)
        {
            Animations = new AnimationSet();
        }


        /// <summary>
        /// Creates a new 2d image actor.
        /// </summary>
        /// <param name="entity">The entity that this actor represents.</param>
        /// <param name="animations">The animation set for the actor.</param>
        public Actor_Image2D(Entity Entity, AnimationSet Animations)
            : base(Entity)
        {
            this.Animations = Animations;
        }


        /// <summary>
        /// Creates a new unanimated actor with the static texture defined.
        /// </summary>
        /// <param name="Entity">The entity that this actor represents.</param>
        /// <param name="TextureName">The name of the texture.</param>
        public Actor_Image2D(Entity Entity, String TextureName) 
            : base(Entity)
        {
            Animations = new AnimationSet(TextureName);
        }

        #endregion


        #region Draw

        /// <summary>
        /// Draws the entity onto the viewport.
        /// </summary>
        /// <param name="spatial">The spatial properties of the actor in world coordinates.</param>
        /// <param name="viewport">The viewport onto which the entity is being rendered.</param>
        /// <param name="time">Provides a snapshot of timing values.</param>
        public override void Draw(Viewport2D viewport, GameTime time)
        {
            // Check early breaks.
            if (viewport.SpriteBatch == null) return;
            if (Animations.Current == null) return;
            if (Entity == null) return;

            // Calculate drawing data.
            Animation Draw_Animation = Animations.Current;
            Vector2 Draw_Position = viewport.Camera.TransformPosition(Entity.SpatialProperties.Position);
            Vector2 Draw_Size = viewport.Camera.TransformSize(Entity.SpatialProperties.Size);
            Vector2 Draw_Origin = new Vector2(Draw_Animation.WidthPerCell / 2, Draw_Animation.HeightPerCell / 2);
            Rectangle Draw_Destination = new Rectangle((int)Draw_Position.X, (int)Draw_Position.Y, (int)Draw_Size.X, (int)Draw_Size.Y);
            Rectangle Draw_Source = Draw_Animation.UpdateSource(time);

            // Draw entity.
            viewport.SpriteBatch.Draw(Draw_Animation.Texture, Draw_Destination, Draw_Source, ColourOverlay, Entity.SpatialProperties.Direction, Draw_Origin, SpriteEffects.None, Depth);
        }

        #endregion

    }
}